# Maintainer: Peter Budai <peterbudai at hotmail.com>

_realname=opencl-icd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=2022.09.30
pkgrel=1
pkgdesc="OpenCL ICD Loader (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/KhronosGroup/OpenCL-ICD-Loader'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-opencl-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
            "${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-ninja"
            "${MINGW_PACKAGE_PREFIX}-tools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/OpenCL-ICD-Loader/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('e9522fb736627dd4feae2a9c467a864e7d25bb715f808de8a04eea5a7d394b74')

build() {
  cd "${srcdir}"/OpenCL-ICD-Loader-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DOPENCL_ICD_LOADER_DISABLE_OPENCLON12=ON \
      -DOPENCL_ICD_LOADER_HEADERS_DIR=${MINGW_PREFIX}/include \
      -DBUILD_TESTING=OFF \
      ../OpenCL-ICD-Loader-${pkgver}

  cmake --build .
}

package() {
  cd "${srcdir}"/build-${CARCH}
  DESTDIR=${pkgdir} cmake --install .
  
  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libOpenCL.dll.a
    ${MINGW_PREFIX}/bin/gendef ${pkgdir}${MINGW_PREFIX}/bin/libOpenCL.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libOpenCL.def -l ${pkgdir}${MINGW_PREFIX}/lib/libOpenCL.dll.a
  fi

  install -Dm644 ${srcdir}/OpenCL-ICD-Loader-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
